/**
 * ...
 * >author		João Vítor de Souza
 */

package com.cupcakeengine.display 
{
	import com.cupcakeengine.core.IDestroyable;
	import com.cupcakeengine.core.IUpdatable;
	import flash.utils.Dictionary;

	public class Animation implements IUpdatable, IDestroyable
	{
		// ___________________________________________________________________ CONSTANTS	
		
		// ___________________________________________________________________ CLASS PROPERTIES	
		
		private var _currentFrame						: int;
		private var _currentLabel						: String;
		private var _isPlaying							: Boolean;		
		
		// ___________________________________________________________________ INSTANCE PROPERTIES
		
		private var _bitmapObject						: BitmapObject;
		private var _inspector							: Dictionary;
		
		// ___________________________________________________________________ GETTERS AND SETTERS
		
		// ___________________________________________________________________ CONSTRUCTOR
		
		public function Animation(bitmapObject:BitmapObject)
		{
			_bitmapObject = bitmapObject;			
			
			_currentFrame = 1;
			_currentLabel = _bitmapObject.label;	
			
			_isPlaying = true;
			
			_inspector = new Dictionary();			
		}		
		
		// ___________________________________________________________________ PUBLIC METHODS		
		
		/* INTERFACE com.cupcakeengine.core.IUpdatable */
		
		public function update():void 
		{
			_bitmapObject.bitmapData = _bitmapObject.currentVector[_currentFrame - 1];
			
			if (!_isPlaying) return;
			
			if (_currentFrame < _bitmapObject.currentVector.length) {				
				_currentFrame++;
			} else {
				if (_inspector[_currentLabel] != null) {
					if (_inspector[_currentLabel] != "") {
						if(_bitmapObject.dBitmapData[_currentLabel] != null){
							_currentLabel = _inspector[_currentLabel];
							_bitmapObject.currentVector = _bitmapObject.dBitmapData[_currentLabel];							
						}
						_currentFrame = 1;
					}
				} else {
					_currentFrame = 1;
				}				
			}
		}
		
		/* INTERFACE com.cupcakeengine.core.IDestroyable */
		
		public function destroy():void 
		{
			_bitmapObject = null;
			_inspector = null;
		}
		
		//
		
		public function setInspector(label:String, loop:String):void
		{
			_inspector[label] = loop;
		}
		
		public function play(label:String):void
		{			
			if (_currentLabel == label) return;
			if (_bitmapObject.dBitmapData[_currentLabel] == null) return;
			
			_currentLabel = label;
			_bitmapObject.currentVector = _bitmapObject.dBitmapData[_currentLabel];
			_currentFrame = 1;
			
			_isPlaying = true;
		}
		
		public function stop(label:String = null):void
		{
			_isPlaying = false;
			
			if (label == null) return;
			if (_currentLabel == label) return;
			if (_bitmapObject.dBitmapData[_currentLabel] == null) return;
			
			_currentLabel = label;
			_bitmapObject.currentVector = _bitmapObject.dBitmapData[_currentLabel];
			_currentFrame = 1;
		}
		
		// ___________________________________________________________________ PRIVATE METHODS
		
		// ___________________________________________________________________ EVENTS
	}
}

